package com.company.笔试.常用排序算法.插入排序;

import com.company.笔试.常用排序算法.array.ArrayData;

import java.util.Arrays;

/**
 * @Classname Solution
 * @Description TODO
 * @Date 2021/9/2 0:34
 * @Created by ylr
 */
public class Solution {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(directSort(new ArrayData().getA())));
        System.out.println("------------------------");
        System.out.println(Arrays.toString(shellSort(new ArrayData().getA())));
    }

    public static int[] directSort(int[] arr){
        for (int i = 1; i < arr.length; i++) {
            for (int j = i; j > 0 ; j--) {
                if (arr[j]>arr[j-1]){
                    arr[j] = arr[j]^arr[j-1];  // a = a^b
                    arr[j-1] = arr[j]^arr[j-1]; //b = a^b 实际上是b=a ^b ^b =a
                    arr[j] = arr[j]^arr[j-1];  //a= a^b 实际上是 a= a^b ^ a = b
                }
            }
        }
        return arr;

    }

    public static int[] shellSort(int[] arr){
        for (int d = arr.length/2; d >0 ; d/=2) {
            for (int i = d  ; i < arr.length ; i++) {
                for (int j = i-d; j >=0 ; j-=d) {
                    if (arr[j]<arr[j+d]){
                        arr[j] = arr[j]^arr[j+d];  // a = a^b
                        arr[j+d] = arr[j]^arr[j+d]; //b = a^b 实际上是b=a ^b ^b =a
                        arr[j] = arr[j]^arr[j+d];  //a= a^b 实际上是 a= a^b ^ a = b
                    }
                }
            }
        }



        return arr;
    }

}
